package com.masterclient.app.modules.order;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.masterclient.app.BaseActivity;
import com.eoner.common.config.Config;
import com.masterclient.app.R;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.baselib.utils.sp.SPUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @auther hjian
 * @date 2019/10/25.
 */
@Route(path = AroutePath.ORDER_SEARCH_RESULT_ACTIVITY)
public class OrderSearchResultActivity extends BaseActivity {


    @BindView(R.id.search_cancel)
    TextView searchCancel;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.search_framelayout)
    FrameLayout searchFramelayoutp;
    @BindView(R.id.iv_clear)
    ImageView ivClear;


    @Autowired(name = "keyword")
     String keyword;

    private List<String> historys;
    private OrderListFragment orderFagment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result_order);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);

        searchEdit.setHint("请输入订单号");
        searchEdit.setText(keyword);
        ivClear.setVisibility(View.VISIBLE);
        String history = SPUtils.getInstance().getString(Config.ORDER_SEARCH_HISTORY);
        if (!TextUtils.isEmpty(history)) {
            historys = new Gson().fromJson(history, new TypeToken<ArrayList<String>>() {
            }.getType());
        }

        setResult(RESULT_OK);


        orderFagment = OrderListFragment.getInstance(OrderListFragment.SEARCH_ORDER,keyword,"user","");
       getSupportFragmentManager().beginTransaction().add(R.id.search_framelayout, orderFagment).commit();

        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                ivClear.setVisibility(searchEdit.getText().toString().length()>0?View.VISIBLE:View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String search = searchEdit.getText().toString().trim();
                    ((InputMethodManager) searchEdit.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    if (TextUtils.isEmpty(search)) {
                        showToast("搜索内容不能为空");
                    } else {
                        keyword = search;
                        orderFagment.searchKeyword(keyword);
                        if (historys.contains(search)) {
                            historys.remove(search);
                        }
                        if (historys.size() == 10) {
                            historys.remove(0);
                        }
                        historys.add(search);
                        SPUtils.getInstance().put(Config.ORDER_SEARCH_HISTORY, new Gson().toJson(historys));
                    }
                    return true;
                }
                return false;
            }
        });
    }

    @OnClick({R.id.search_cancel, R.id.iv_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_cancel:
                finish();
                break;
            case R.id.iv_clear:
                searchEdit.getText().clear();
                break;
        }
    }
}
